<?php

$TPL_PATH = dirname(__FILE__) . "/tpl/";
$PHP_PATH = dirname(__FILE__) . "/php/";

if(isset($_POST['ajax']) && isset($_POST['iId']) && FormValidator::IsInt($_POST['iId'])){
	
	$q = "SELECT id, crop_img1, name, descr FROM tb_gallery_photo WHERE id = '{$_POST['iId']}'";
	
	$data = mysql_fetch_assoc(mysql_query($q));
	
	if(isset($data['crop_img1'])){
		$medium_photo = strtr(file_get_contents($TPL_PATH . 'main.medium_photo.tpl'),
				array(
				'{ID}' => $data['id'],
				'{IMAGE}' => $data['crop_img1'],
				'{NAME}' => $data['name'],
				'{DESCR}' => $data['descr'],
					)
				);
		$ajax = new AjaxResponse();
		$ajax->SetCallbackJsFunc("ajaxCallBack", $medium_photo);
		//$ajax->InnerHTML('big-img', $medium_photo);
		echo $ajax->Out();
	}
	exit();			
}
if(isset($_POST['ajax']) && isset($_POST['prevId']) && FormValidator::IsInt($_POST['prevId'])){
	
	$q = "SELECT parent_id FROM tb_gallery_photo WHERE id = '{$_POST['prevId']}'";
	$data = mysql_fetch_assoc(mysql_query($q));
	if(isset($data['parent_id'])){
	
		$q = "SELECT id, crop_img1, name, descr FROM tb_gallery_photo WHERE parent_id = '{$data['parent_id']}'";
		$r = mysql_query($q);
		$photos = array();
		while($data = mysql_fetch_assoc($r)){
			$photos[] = $data;
		}	

		foreach ($photos as $k => $photo){
			
			if($photo['id'] == $_POST['prevId']){
				
				$data = (isset($photos[$k+1]))?$photos[$k+1]:$photos[0];
				
				if(isset($data['crop_img1'])){
					$medium_photo = strtr(file_get_contents($TPL_PATH . 'main.medium_photo.tpl'),
							array(
							'{ID}' => $data['id'],
							'{IMAGE}' => $data['crop_img1'],
							'{NAME}' => $data['name'],
							'{DESCR}' => $data['descr'],
								)
							);
					$ajax = new AjaxResponse();
					$ajax->SetCallbackJsFunc("ajaxCallBack", $medium_photo);
					//$ajax->InnerHTML('big-img', $medium_photo);
					echo $ajax->Out();
					exit();
				}
			}	
			
		}
	}
	exit();			
}
if(isset($_POST['ajax']) && isset($_POST['nextId']) && FormValidator::IsInt($_POST['nextId'])){
	
	$q = "SELECT parent_id FROM tb_gallery_photo WHERE id = '{$_POST['nextId']}'";
	$data = mysql_fetch_assoc(mysql_query($q));
	
	if(isset($data['parent_id'])){
	
		$q = "SELECT id, crop_img1, name, descr FROM tb_gallery_photo WHERE parent_id = '{$data['parent_id']}'";
		$r = mysql_query($q);
		$photos = array();
		while($data = mysql_fetch_assoc($r)){
			$photos[] = $data;
		}	
		
		
		foreach ($photos as $k => $photo){
			
			if($photo['id'] == $_POST['nextId']){
				
				$data = (isset($photos[$k-1]))?$photos[$k-1]:$photos[sizeof($photos) - 1];
				
				if(isset($data['crop_img1'])){
					$medium_photo = strtr(file_get_contents($TPL_PATH . 'main.medium_photo.tpl'),
							array(
							'{ID}' => $data['id'],
							'{IMAGE}' => $data['crop_img1'],
							'{NAME}' => $data['name'],
							'{DESCR}' => $data['descr'],
								)
							);
					$ajax = new AjaxResponse();
					$ajax->SetCallbackJsFunc("ajaxCallBack", $medium_photo);
					//$ajax->InnerHTML('big-img', $medium_photo);
					echo $ajax->Out();
					exit();
				}
			}				
		}
	}
	exit();			
}


if(!isset($_GET['pId']) || !FormValidator::IsInt($_GET['pId'])){
	header("Location:/");
	exit();
}

$gallery = Gallery::getInstanse($_GET['pId']);

$photos = '';
$medium_photo = '';
$tpl_photo = file_get_contents($TPL_PATH . 'main_row.htm');

$i = 0;

foreach ($gallery->getPhotos() as $val){
	
	if($i == 0){
		$medium_photo = strtr(file_get_contents($TPL_PATH . 'main.medium_photo.tpl'),
			array(
			'{ID}' => $val['id'],
			'{IMAGE}' => $val['crop_img1'],
			'{NAME}' => $val['name'],
			'{DESCR}' => $val['descr'],
				)
			);
	}
	
	$photos .= strtr($tpl_photo,
		array(
		'{IMG}' => $val['crop_img'],
		'{ID}' => $val['id'],
			)
		);
	$i++;	
}

$content = strtr(file_get_contents($TPL_PATH . "main.htm"),
	array(
	'{ROWS}' => $photos,
	'{MEDIUM_PHOTO}' => $medium_photo,
		)
	);

$out = strtr(file_get_contents("template/main.tpl"),
	array(
	'{CONTENT}' => $content,
		)
	);